package com.stu.dao;

import com.stu.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;


import java.util.List;

@Repository
public class StudentImp implements StudentDao{

    @Autowired
    private  JdbcTemplate jdbcTemplate;



@Deprecated
    public StudentImp(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Override
    public int insert (Integer num,String name,String grade,String gender){
        String sql="insert into students(num,name,grade,gender) values(?,?,?,?)";
        return jdbcTemplate.update(sql,num,name,grade,gender);
    }

    @Override
    public List<Student> FindAll() {
        String sql="select * from students";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(Student.class));
    }

    @Override
    public int updateByName(String name,String num,String grade,String gender){
        String sql="update students set num=?,name=?,grade=?,gender=? where name=?";
        return jdbcTemplate.update(sql,num,name,grade,gender,name);
    }
    @Override
    public int deleteByNumber(String num){
        String sql="delete from students where num=?";
        return jdbcTemplate.update(sql,num);
    }
    @Override
    public Student findByNumber(String num){
        String sql="select * from students where num=?";
        return jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<>(Student.class));


        }
    }

